<?php
namespace catchAdmin\externalGoods\model;

use catcher\base\CatchModel;
use think\facade\Db;

class DisabledCategories extends CatchModel
{
    protected $name = 'db_syst_disabled_categories';
    
    protected $field = [
        'id',
        'category_id',
        'created_at',
        'updated_at',
        'deleted_at',
        'creator_id'
    ];

    /**
     * 获取所有禁用的分类ID
     */
    public function getDisabledCategoryIds()
    {
        return $this->where('deleted_at', null)
            ->column('category_id');
    }

    /**
     * 批量设置禁用分类
     */
    public function setDisabledCategories(array $categoryIds, int $creatorId)
    {
        // 开启事务
        $this->startTrans();
        try {
            // 使用Db类直接删除表中所有数据
            Db::table($this->name)->delete(true);

            // 插入新记录
            $data = [];
            foreach ($categoryIds as $categoryId) {
                $data[] = [
                    'category_id' => $categoryId,
                    'creator_id' => $creatorId
                ];
            }
            if (!empty($data)) {
                $this->insertAll($data);
            }

            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->rollback();
            throw $e;
        }
    }
} 